Add some specs to compensate for the removed specs for generate_uri.

Akinori MUSHA 10 年之前
父节点
当前提交
7285cba8a5
共有 1 个文件被更改,包括 44 次插入18 次删除
  1. 44 18
      spec/models/agents/post_agent_spec.rb

+ 44 - 18
spec/models/agents/post_agent_spec.rb

@@ -1,4 +1,5 @@
1 1
 require 'spec_helper'
2
+require 'ostruct'
2 3
 
3 4
 describe Agents::PostAgent do
4 5
   before do
@@ -32,22 +33,20 @@ describe Agents::PostAgent do
32 33
     stub_request(:any, /:/).to_return { |request|
33 34
       method = request.method
34 35
       @requests += 1
36
+      @sent_requests[method] << req = OpenStruct.new(uri: request.uri)
35 37
       case method
36 38
       when :get, :delete
37
-        data = request.uri.query
39
+        req.data = request.uri.query
38 40
       else
39
-        if data = request.body
40
-          case request.headers['Content-Type'][/\A[^;\s]+/]
41
-          when 'application/x-www-form-urlencoded'
42
-            # ok
43
-          when 'application/json'
44
-            data = ActiveSupport::JSON.decode(data)
45
-          else
46
-            raise "unexpected Content-Type: #{content_type}"
47
-          end
41
+        case request.headers['Content-Type'][/\A[^;\s]+/]
42
+        when 'application/x-www-form-urlencoded'
43
+          req.data = request.body
44
+        when 'application/json'
45
+          req.data = ActiveSupport::JSON.decode(request.body)
46
+        else
47
+          raise "unexpected Content-Type: #{content_type}"
48 48
         end
49 49
       end
50
-      @sent_requests[method] << data
51 50
       { status: 200, body: "ok" }
52 51
     }
53 52
   end
@@ -82,8 +81,8 @@ describe Agents::PostAgent do
82 81
         }.should change { @sent_requests[:post].length }.by(2)
83 82
       }.should_not change { @sent_requests[:get].length }
84 83
 
85
-      @sent_requests[:post][0].should == @event.payload.merge('default' => 'value').to_query
86
-      @sent_requests[:post][1].should == event1.payload.to_query
84
+      @sent_requests[:post][0].data.should == @event.payload.merge('default' => 'value').to_query
85
+      @sent_requests[:post][1].data.should == event1.payload.to_query
87 86
     end
88 87
 
89 88
     it "can make GET requests" do
@@ -95,7 +94,20 @@ describe Agents::PostAgent do
95 94
         }.should change { @sent_requests[:get].length }.by(1)
96 95
       }.should_not change { @sent_requests[:post].length }
97 96
 
98
-      @sent_requests[:get][0].should == @event.payload.merge('default' => 'value').to_query
97
+      @sent_requests[:get][0].data.should == @event.payload.merge('default' => 'value').to_query
98
+    end
99
+
100
+    it "can make a GET request merging params in post_url, payload and event" do
101
+      @checker.options['method'] = 'get'
102
+      @checker.options['post_url'] = "http://example.com/a/path?existing_param=existing_value"
103
+      @event.payload = {
104
+        "some_param" => "some_value",
105
+        "another_param" => "another_value"
106
+      }
107
+      @checker.receive([@event])
108
+      uri = @sent_requests[:get].first.uri
109
+      # parameters are alphabetically sorted by Faraday
110
+      uri.request_uri.should == "/a/path?another_param=another_value&default=value&existing_param=existing_value&some_param=some_value"
99 111
     end
100 112
 
101 113
     it "can skip merging the incoming event when no_merge is set, but it still interpolates" do
@@ -104,7 +116,21 @@ describe Agents::PostAgent do
104 116
         'key' => 'it said: {{ someotherkey.somekey }}'
105 117
       }
106 118
       @checker.receive([@event])
107
-      @sent_requests[:post].first.should == { 'key' => 'it said: value' }.to_query
119
+      @sent_requests[:post].first.data.should == { 'key' => 'it said: value' }.to_query
120
+    end
121
+
122
+    it "interpolates when receiving a payload" do
123
+      @checker.options['post_url'] = "https://{{ domain }}/{{ variable }}?existing_param=existing_value"
124
+      @event.payload = {
125
+        'domain' => 'google.com',
126
+        'variable' => 'a_variable'
127
+      }
128
+      @checker.receive([@event])
129
+      uri = @sent_requests[:post].first.uri
130
+      uri.scheme.should == 'https'
131
+      uri.host.should == 'google.com'
132
+      uri.path.should == '/a_variable'
133
+      uri.query.should == "existing_param=existing_value"
108 134
     end
109 135
   end
110 136
 
@@ -114,7 +140,7 @@ describe Agents::PostAgent do
114 140
         @checker.check
115 141
       }.should change { @sent_requests[:post].length }.by(1)
116 142
 
117
-      @sent_requests[:post][0].should == @checker.options['payload'].to_query
143
+      @sent_requests[:post][0].data.should == @checker.options['payload'].to_query
118 144
     end
119 145
 
120 146
     it "sends options['payload'] as JSON as a POST request" do
@@ -123,7 +149,7 @@ describe Agents::PostAgent do
123 149
         @checker.check
124 150
       }.should change { @sent_requests[:post].length }.by(1)
125 151
 
126
-      @sent_requests[:post][0].should == @checker.options['payload']
152
+      @sent_requests[:post][0].data.should == @checker.options['payload']
127 153
     end
128 154
 
129 155
     it "sends options['payload'] as a GET request" do
@@ -134,7 +160,7 @@ describe Agents::PostAgent do
134 160
         }.should change { @sent_requests[:get].length }.by(1)
135 161
       }.should_not change { @sent_requests[:post].length }
136 162
 
137
-      @sent_requests[:get][0].should == @checker.options['payload'].to_query
163
+      @sent_requests[:get][0].data.should == @checker.options['payload'].to_query
138 164
     end
139 165
   end
140 166